﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;

namespace L4Alive.Module.Controllers
{
    [NonPersistent, ModelDefault("Caption", "Log On")]
    public class CustomLogonParameters : ICustomObjectSerialize
    {

        [Browsable(false)]
        //public User user;
        public SecuritySystemUser user;
        //[DataSourceProperty("AvailableUsers"), ImmediatePostData]

        private string login;
        // [ImmediatePostData]
        [System.ComponentModel.DisplayName("Uživatelské jméno")]
        public string LOGIN
        {
            get { return login; }
            set
            {
                login = value;
                user = ObjectSpace.FindObject<SecuritySystemUser>(new BinaryOperator("UserName", login, BinaryOperatorType.Equal));
                if (user != null)
                {
                    UserName = user.UserName;
                    Password = "";
                }
                else
                { //UserName = string.Empty;
                    UserName = value;

                }
            }
        }

        [PasswordPropertyText(true)]
        public string Password { get; set; }
        private IObjectSpace objectSpace;

        [Browsable(false)]
        public IObjectSpace ObjectSpace
        {
            get { return objectSpace; }
            set { objectSpace = value; }
        }

        public void Reset()
        {
            ObjectSpace = null;

            user = null;
            Password = null;
        }
        [Browsable(false)]
        public string UserName { get; set; }
        public void ReadPropertyValues(SettingsStorage storage)
        {
            if (storage != null)
            {
                user = objectSpace.FindObject<SecuritySystemUser>(
                    new BinaryOperator("UserName", storage.LoadOption("", "UserName")));
                if (user != null)
                {
                    login = user.UserName;
                    UserName = user.UserName;
                }
            }
        }

        public void WritePropertyValues(SettingsStorage storage)
        {
            if (storage != null)
                storage.SaveOption("", "UserName", UserName);
        }
    }

}
